package cn.starrysky.hrm.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import javax.sql.DataSource;
import java.util.Arrays;

//授权服务器配置
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    //密碼編碼器
    @Autowired
    private PasswordEncoder passwordEncoder;

    //第一步：客户端详情配置======================================================================================
    //客户端详细信息服务配置：客戶端id,客戶端秘钥，授权方式等
    /**
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //添加客户端信息
        // 使用in-memory存储客户端信息，存储到内存中
        clients.inMemory()
                // client_id    客户端id
                .withClient("webapp")
                // client_secret    客户端密钥，使用密文
                .secret(passwordEncoder.encode("secret"))
                //资源id，设定资源服务访问的资源的id
                .resourceIds("res1")
                // 该client允许的授权类型：授权码
                .authorizedGrantTypes("client_credentials","password","authorization_code","refresh_token")
                // 允许的授权范围
                .scopes("all")
                //false，申请令牌的时候跳转到授权页面让用户确认，如果为true回直接发令牌
                .autoApprove(false)
                .redirectUris("http://www.baidu.com") ;   //回调地址
    }
    **/


    @Autowired
    private DataSource dataSource ;
    //定义针对于数据库的客户端配置详情服务
    @Bean
    public ClientDetailsService clientDetailsService( ){
        JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);
        //设置密码编码
        jdbcClientDetailsService.setPasswordEncoder(passwordEncoder);
        return jdbcClientDetailsService;
    }

    //修改ClientDetailsServiceConfigurer 使用基于jdbc的客户端详情配置方案
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        //配置成基于jdbc的客户端详情方案
        clients.withClientDetails(clientDetailsService());
    }


    //第二步：令牌服务配置======================================================================================

    //设置JWT签名密钥。它可以是简单的MAC密钥，也可以是RSA密钥
    private final String sign_key  = "123";

    //令牌存储 ， 基于内存
    @Bean
    public TokenStore tokenStore(){
        //return new InMemoryTokenStore();
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    //JWT令牌校验工具
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter(){
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        //设置JWT签名密钥。它可以是简单的MAC密钥，也可以是RSA密钥
        jwtAccessTokenConverter.setSigningKey(sign_key);
        return jwtAccessTokenConverter;
    }


    //客户端详情service
    @Autowired
    private ClientDetailsService clientDetailsService;

    //配置令牌服务
    @Bean
    public AuthorizationServerTokenServices tokenService(){
        //创建默认的令牌服务
        DefaultTokenServices services = new DefaultTokenServices();
        //指定客户端详情配置
        services.setClientDetailsService(clientDetailsService);
        //支持产生刷新token
        services.setSupportRefreshToken(true);

        //设置token增强 - 设置token转换器
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(Arrays.asList(jwtAccessTokenConverter()));
        services.setTokenEnhancer(tokenEnhancerChain);  //jwtAccessTokenConverter()

        //token存储方式
        services.setTokenStore(tokenStore());
        //token有效时间2小时
        services.setAccessTokenValiditySeconds(72000);
        //刷新令牌默认有效时间
        services.setRefreshTokenValiditySeconds(200000);
        return services;
    }

    //第三步；配置令牌端点======================================================================================

    //认证管理器,在WebSecurityConfig中配置
    @Autowired
    private AuthenticationManager authenticationManager;

    //配置令牌访问端点
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints
            //密码授权模式需要
            .authenticationManager(authenticationManager)
            //授权码模式服务
            .authorizationCodeServices(authorizationCodeServices())
            //配置令牌管理服务
            .tokenServices(tokenService())
            //允许post方式请求
            .allowedTokenEndpointRequestMethods(HttpMethod.POST);

    }

    //授权码服务的bean
    @Bean
    public AuthorizationCodeServices authorizationCodeServices(){
        //基于内存存储的的授权码服务
        //return new InMemoryAuthorizationCodeServices();
        //基于数据库存储的的授权码服务
        return new JdbcAuthorizationCodeServices(dataSource);
    }


    //第四步：端点安全约束======================================================================================
    //配置令牌安全约束
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security
            //对应/oauth/token_key 公开
            .tokenKeyAccess("permitAll()")
            //对应/oauth/check_token ，路径公开
            .checkTokenAccess("permitAll()")
            //允许客户端进行表单身份验证,使用表单认证申请令牌
            .allowFormAuthenticationForClients();
    }


}